package com.defysoft.waukids.main;

import org.json.JSONObject;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.defysoft.waukids.BaseActivity;
import com.defysoft.waukids.Data_Sharedpreferences;
import com.defysoft.waukids.Data_Type.Member_Detail;
import com.defysoft.waukids.Data_Type.School_Detail;
import com.defysoft.waukids.JsonParser;
import com.defysoft.waukids.PublicSharedpreferences;
import com.defysoft.waukids.PublicSharedpreferences.SF_Member_Key;
import com.defysoft.waukids.PublicSharedpreferences.SF_School_Key;
import com.defysoft.waukids.R;
import com.defysoft.waukids.Sqlite_Connect;

public class Main_Login extends BaseActivity implements OnClickListener{
	EditText et[] = new EditText[2];
	ImageView iv[] = new ImageView[2];
	TextView tv;
	Sqlite_Connect sqlite_Connect;
	String email = "", pass = "", regId = "";
	String memberjson = "";
	String schooljson="";
	School_Detail school_detail;
	Member_Detail member_detail;
	PublicSharedpreferences psf;
	SharedPreferences.Editor editor,scheditor;
	Data_Sharedpreferences.SF_Member member;
	private final int SUCCESS = 10;
	private final int FAIL = 9;
	private final int NETWORK_ERR = 8;

	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.main_login_activity);
		regId = getIntent().getStringExtra("MEM_UUID");
		
		setStartData();
	}

	@Override
	public void setStartData() {
		super.setStartData();
		psf = new PublicSharedpreferences(this);
		psf.setMembersp();
		member = psf.getMembersp();
		sqlite_Connect = new Sqlite_Connect(this);
		et[0] = (EditText) findViewById(R.id.MAIN_LOGIN_ID);
		et[1] = (EditText) findViewById(R.id.MAIN_LOGIN_PASS);
		iv[0] = (ImageView) findViewById(R.id.MAIN_LOGIN_LOGIN);
		tv = (TextView) findViewById(R.id.MAIN_LOGIN_FINDPASS);
	
		iv[0].setOnClickListener(this);
		((TextView) findViewById(R.id.MAIN_LOGIN_JOIN)).setOnClickListener(this);
		tv.setOnClickListener(this);		
	}
		
	@Override
	public void startThread(int choice) {
		super.startThread(choice);
		final int cho= choice;
		firstThread = new Thread(new Runnable() {			
			@Override
			public void run() {
				switch (cho) {
				case 1:
					mainThread();
					break;
				}
			}
		});
		firstThread.start();
		startDIal();
	}

	public void mainThread(){
		email = et[0].getText().toString();
		pass = et[1].getText().toString();
		
		try {
			
			JSONObject jsonObject = new JSONObject(publicHttpCallback(HTTP_URL,
					"code=" + CODE_MAIN_LOGIN + "&email=" + email
							+ "&pass=" + pass + "&uuid=" + regId));
			
			if (jsonObject.getBoolean("success")) {
				memberjson = jsonObject.getString("payload");
				schooljson = jsonObject.getString("sch");				
				onNetworkCallback(SUCCESS, "");				
			} else {
				onNetworkCallback(FAIL, jsonObject.getString("message"));
			}
		} catch (Exception e) {
			onNetworkCallback(NETWORK_ERR, e.toString()+"where mainThread()");
		}
	}
	
	@Override
	protected void handlingMessage(Message _message) {
		super.handlingMessage(_message);

		switch (_message.what) {
		case SUCCESS:	
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			member_detail = JsonParser.member_data(memberjson);
			school_detail = JsonParser.school_data(schooljson);	
			
			if(!member_detail.equals("") && !school_detail.equals("") ){
					setShare();
			}
			
			Intent intent;			
			if(member_detail.Mem_Connect.equals("0")){
				intent = new Intent(this,Main_ChangePass.class);
				intent.putExtra("MEM_EMAIL", member_detail.PK_Mem_Email);
				startActivity(intent);
				finish();
			}else{
				intent = new Intent(this,Main_MainFrame.class);
				startActivity(intent);
				finish();
			}
			break;

		case FAIL:
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
				Toast.makeText(this, (String)_message.obj, Toast.LENGTH_SHORT).show();
			break;
			
		case NETWORK_ERR:	
			if (firstThread != null) {
				firstThread.interrupt();
			}
			endDIal();
			String log_email ="";
			if(member.Email.equals("")){
				log_email = et[0].getText().toString();
			}else{
				log_email = member.Email;
			}
				sqlite_Connect.insert_Err_Log((String)_message.obj,android.os.Build.VERSION.SDK_INT,"Main_Login",log_email );
			break;	
		}
	}

	@Override
	public void onClick(View v) {
		Intent i;
		switch (v.getId()) {
		case R.id.MAIN_LOGIN_FINDPASS:
			i = new Intent(this, Main_SearchMember.class);
			startActivity(i);
			break;

		case R.id.MAIN_LOGIN_LOGIN:
			startNetwork(1);
			break;
			
		case R.id.MAIN_LOGIN_JOIN:
			i = new Intent(this, Main_JoinYacguan.class);
			i.putExtra("MEM_UUID", regId);
				startActivity(i);
			break;
		}
	}
	
	//SharePreference
	void setShare(){
		psf = new PublicSharedpreferences(this);
		editor = psf.getMember_edit();
		editor.putString(SF_Member_Key.EMAIL, member_detail.PK_Mem_Email);
		editor.putString(SF_Member_Key.PASS, pass);
		editor.putString(SF_Member_Key.SCHOOL, member_detail.FK_Sch_Mem_Code);
		editor.putString(SF_Member_Key.CLASS, member_detail.FK_Cla_Mem_Code);
		editor.putString(SF_Member_Key.NAME, member_detail.Mem_Name);
		editor.putString(SF_Member_Key.CREATEDATE, member_detail.Mem_CreateDate);
		editor.putString(SF_Member_Key.UUID, member_detail.Mem_Uuid);
		editor.putString(SF_Member_Key.CONNECT, member_detail.Mem_Connect);
		editor.putString(SF_Member_Key.Rank, member_detail.Mem_Rank);
		editor.commit();
		
		editor = psf.getSchool_edit();
		editor.putString(SF_School_Key.CODE, school_detail.PK_Sch_Code);
		editor.putString(SF_School_Key.ADDRESS_Code, school_detail.FK_Add_Sch_Code);
		editor.putString(SF_School_Key.NAME, school_detail.Sch_Name);
		editor.putString(SF_School_Key.ADDRESS, school_detail.Sch_Add);
		editor.putString(SF_School_Key.LOGO, school_detail.Sch_Logo);
		editor.putString(SF_School_Key.DELETE, school_detail.IsDelete);
		editor.putString(SF_School_Key.DOCUMENT, school_detail.Sch_Document);
		editor.putString(SF_School_Key.CCTV, school_detail.Sch_Cctv);
		editor.putString(SF_School_Key.CCTVTIMESTART, school_detail.Sch_Cctv_Time_Start);
		editor.putString(SF_School_Key.CCTVTIMESTOP, school_detail.Sch_Cctv_Time_Stop);
		editor.commit();		
	}
	
}
